/* ************************************************************
   Nuevas tablas para definir grupos de Ctas Ctbles

   Ale - 25/11/2020
  ************************************************************ */

-- =============================
-- tabla public.ctbimp
-- =============================
CREATE TABLE public.ctbimp
(
  cod character varying(50) NOT NULL,
  nombre character varying(100) NOT NULL,
  idambdb integer,
  fechainsert timestamp without time zone,
  fechaupdate timestamp without time zone,
  CONSTRAINT ctbimp_pkey PRIMARY KEY (cod)
 )
WITH (OIDS=TRUE);
ALTER TABLE public.ctbimp OWNER TO abacouser;

COMMENT ON TABLE  public.ctbimp IS 'Grupos de Ctas Ctbles';
COMMENT ON COLUMN public.ctbimp.cod IS 'Cod del grupo';
COMMENT ON COLUMN public.ctbimp.nombre IS 'Nombre descriptivo del grupo';

--  Trigger auditoria
CREATE TRIGGER control_audit_ctbimp
  BEFORE INSERT OR UPDATE OR DELETE
  ON public.ctbimp
  FOR EACH ROW
  EXECUTE PROCEDURE public.tg_control_audit_instancia();

-- definir unicidad de cod por lower case
CREATE UNIQUE INDEX ctbimp_lower_cod
  ON public.ctbimp
  USING btree
   (lower(btrim(cod::character varying)) COLLATE pg_catalog."default");

-- =============================
-- tabla public.ctbimpdet
-- =============================
CREATE SEQUENCE seq_ctbimpdet
  INCREMENT 1
  MINVALUE 1
  MAXVALUE 9223372036854775807;

CREATE TABLE public.ctbimpdet
(
  id integer NOT NULL DEFAULT nextval('seq_ctbimpdet'::regclass),
  codctbimp character varying(50) NOT NULL,
  idctb integer NOT NULL,
  idambdb integer,
  fechainsert timestamp without time zone,
  fechaupdate timestamp without time zone,
  CONSTRAINT ctbimpdet_pkey PRIMARY KEY (id),
  CONSTRAINT gestion_ai_ctbimpdet_ctbimp FOREIGN KEY (codctbimp)
      REFERENCES public.ctbimp (cod) MATCH SIMPLE
      ON UPDATE CASCADE ON DELETE CASCADE,
  CONSTRAINT gestion_ai_ctbimpdet_idctb FOREIGN KEY (idctb)
      REFERENCES public.ctb (id) MATCH SIMPLE
      ON UPDATE CASCADE ON DELETE RESTRICT,
  CONSTRAINT ctbimpdet_unique UNIQUE (codctbimp, idctb)
 )
WITH (OIDS=TRUE);
ALTER TABLE public.ctbimpdet OWNER TO abacouser;

COMMENT ON TABLE  public.ctbimpdet IS 'Cuentas contables de los Grupos de Ctas Ctbles';
COMMENT ON COLUMN public.ctbimpdet.codctbimp IS 'Codigo del grupo de Ctas. Ctbles';

--  Trigger auditoria
CREATE TRIGGER control_audit_ctbimpdet
  BEFORE INSERT OR UPDATE OR DELETE
  ON public.ctbimpdet
  FOR EACH ROW
  EXECUTE PROCEDURE public.tg_control_audit_instancia();


--=====================================
SELECT aaVersion('445.02','445.01');
--=====================================
